//
// Created by leisurexi on 9/28/20.
// Te grace close server.
//
#include <dirent.h>
#include "lib/common.h"
#include "sys/file.h"

static int count;

static void sig_int(int signo) {
    printf("\nreceived %d datagram\n", count);
    exit(0);
}

static void sig_pipe(int signo) {
    printf("\nreceived %d datagram\n", count);
    exit(0);
}

int main(int argc, char **argv) {
    int listenfd;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(SERV_PORT);

    int on = 1;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    int rt1 = bind(listenfd, (struct sockaddr *) &server_addr, sizeof(server_addr));
    if (rt1 < 0)
        error(1, errno, "bind failed");

    int rt2 = listen(listenfd, LISTENQ);
    if (rt2 < 0)
        error(1, errno, "listen failed");

//    signal(SIGINT, sig_int);
//    signal(SIGPIPE, SIG_IGN);
//    signal(SIGINT, sig_pipe);

    int connfd;
    struct sockaddr_in client_addr;
    socklen_t client_len = sizeof(client_addr);

    while ((connfd = accept(listenfd, (struct sockaddr *) &client_addr, &client_len)) >= 0) {
        char message[MAXLINE];
        struct dirent *ent = NULL;
        count = 0;
        for (;;) {
            int n = read(connfd, message, MAXLINE);
            if (n < 0) {
                error(1, errno, "error read");
            } else if (n == 0) {
                printf("client close\n");
                break;
//            error(1, 0, "client close \n");
            }
            message[n] = 0;
            printf("received %d bytes: %s\n", n, message);
            count++;

            char send_line[MAXLINE];

//        sprintf(send_line, "Hi, %s", message);

//        sleep(5);

            if (strncmp(message, "pwd", 3) == 0) {
                getcwd(send_line, MAXLINE);
            } else if (strncmp(message, "cd", 2) == 0) {
                char path[MAXLINE];
                strncpy(path, &message[3], MAXLINE - 3);
                chdir(path);
                getcwd(send_line, MAXLINE);
            } else if (strncmp(message, "ls", 2) == 0) {
                char path[MAXLINE];
                getcwd(path, MAXLINE);
                DIR *p_dir = opendir(path);
                if (p_dir == NULL)
                    error(1, errno, "can not open dir");

                while ((ent = readdir(p_dir)) != NULL) {
                    strcat(send_line, ent->d_name);
                    strcat(send_line, "\n");
                }
            }

            int write_nc = send(connfd, send_line, strlen(send_line), 0);
            printf("send bytes: %zu \n", write_nc);
            if (write_nc < 0)
                error(1, errno, "error write");
        }
    }

//    if ((connfd = accept(listenfd, (struct sockaddr *) &client_addr, &client_len)) < 0)
//        error(1, errno, "accept failed");


}

